package com.huluobo.a2301mdmdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.ui.Modifier
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2301mdmdemo.pages.MainPage
import com.huluobo.a2301mdmdemo.pages.WelcomePage
import com.huluobo.a2301mdmdemo.ui.theme._2301MDMDemoTheme
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.POST_NOTIFICATIONS
            ), 102
        )

        setContent {
            _2301MDMDemoTheme {
                // A surface container using the 'background' color from the theme
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    //起始页的路径,默认进入欢迎页
                    var startPage = RoutePath.WELCOME
                    //获取mmkv
                    val mmkv = MMKV.defaultMMKV()
                    //首页和欢迎页使用单独的一个导航控制器,和主页的导航控制器不是一个
                    val navHostController = rememberNavController()

                    //通过mmkv读取是否进入过app,如果读取不到默认就是false
                    val isIn = mmkv.decodeBool(Const.PARAM_IS_IN, false)
                    if (isIn) {
                        //如果进入过app,设置起始页为首页
                        startPage = RoutePath.MAIN
                    } else {
                        lifecycleScope.launch {
                            //通过async异步来延时5秒执行 await 会等待之前的异步执行完毕才会继续执行代码
                            async { delay(5000) }.await()
                            //等待完成,进入到首页
                            navHostController.navigate(RoutePath.MAIN)
                            //mmkv保存已经进入过app
                            mmkv.encode(Const.PARAM_IS_IN, true)
                        }
                    }

                    NavHost(navController = navHostController, startDestination = startPage, builder = {
                        composable(RoutePath.MAIN) { MainPage() }
                        composable(RoutePath.WELCOME) { WelcomePage() }
                    })
                }
            }
        }
    }
}